package ldkxingzhe.ldkxingzhedemo.webview;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.io.IOException;
import java.net.CookieManager;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import ldkxingzhe.ldkxingzhedemo.R;
import ldkxingzhe.ldkxingzhedemo.service.AsyncTaskActivity;
import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by liangdengke on 2015/9/6.
 */
public class WebViewFirstActivity extends AppCompatActivity{

    private final String TAG = WebViewFirstActivity.class.getSimpleName();

    private WebView mWebView;
    private RequestQueue requestQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view_first);
        mWebView = (WebView)findViewById(R.id.web_view);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LogDemo.v(TAG, "url = " + url);
                return false;
            }
        });
        MyTask myTask = new MyTask();
        myTask.execute("http://192.168.100.198:7001/portal/login");

/*        CookieSyncManager.createInstance(this);
        android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.removeSessionCookie();//移除
        cookieManager.setCookie("192.168.100.198:7001", "CASTGC=TGT-68-LuyeBOR1ujI7XAXbN76avQgGgdMG6j0iFIEnyoc3x5n5i9QJUs-liyong; path=/");//cookies是在HttpClient中获得的cookie
        CookieSyncManager.getInstance().sync();

        mWebView.loadUrl("http://192.168.100.198:7001/ResWebGIS/res/inspect/analysisChart!terminalChartEnter.action?userid=by");*/
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()){
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class MyTask extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            String result = null;
            String url = params[0];
            try {
                URL mURL = new URL(url);
                HttpURLConnection connection = (HttpURLConnection)mURL.openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
                connection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8,en;q=0.6");
                connection.setRequestProperty("Upgrade-Insecure-Requests","1");
                connection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
                connection.setRequestProperty("Connection", "keep-alive");
                connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36");
                Map<String, List<String>>requestMap =  connection.getRequestProperties();
                for(String tmp : requestMap.keySet()){
                    for(String header : requestMap.get(tmp)){
                        Log.v(TAG,"request header " + tmp + " is " + header);
                    }
                }

                Map<String,List<String>> map = connection.getHeaderFields();
                for(String str : map.get("Set-Cookie")) {
                    Log.v(TAG,"str is " + str);
                    result = str;
                }
                for(String tmp : map.keySet()){
                    for(String header : map.get(tmp)){
                        Log.v(TAG,"header " + tmp + " is " + header);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            CookieSyncManager.createInstance(WebViewFirstActivity.this);
            android.webkit.CookieManager cookieManager = android.webkit.CookieManager.getInstance();
            cookieManager.setAcceptCookie(true);
            cookieManager.removeSessionCookie();//移除
            Log.v(TAG, "s is " + s);
            cookieManager.setCookie("192.168.100.198:7001", s);//cookies是在HttpClient中获得的cookie
            CookieSyncManager.getInstance().sync();

            mWebView.loadUrl("http://192.168.0.79:8001/ResWebGIS/res/analyse/totalChart!terminalChartEnter.action?userid=by");
            super.onPostExecute(s);
        }
    }
}
